package com.pcg.posgasstation.view;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.pcg.posgasstation.GlideApp;
import com.pcg.posgasstation.R;
import com.pcg.posgasstation.base.BaseActivity;
import com.pcg.posgasstation.model.Constant;
import com.pcg.posgasstation.model.network.ApiClient;
import com.pcg.posgasstation.model.network.entity.BaseEntity;
import com.pcg.posgasstation.model.network.entity.OrderDetailEntity;
import com.pcg.posgasstation.utils.ToastUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class OrderDetailActivity extends BaseActivity {

    @BindView(R.id.layTvBack)
    ImageView mLayTvBack;
    @BindView(R.id.layBack)
    RelativeLayout mLayBack;
    @BindView(R.id.layTvTitle)
    TextView mLayTvTitle;
    @BindView(R.id.tvTotalMoney)
    TextView mTvTotalMoney;
    @BindView(R.id.tvRealMoney)
    TextView mTvRealMoney;
    @BindView(R.id.tvDiscountAmount)
    TextView mTvDiscountAmount;
    @BindView(R.id.tvBuyAmount)
    TextView mTvBuyAmount;
    @BindView(R.id.tvGoodName)
    TextView mTvGoodName;
    @BindView(R.id.tvGoodPrice)
    TextView mTvGoodPrice;
    @BindView(R.id.tvDiscountMoney)
    TextView mTvDiscountMoney;
    @BindView(R.id.tvMemberName)
    TextView mTvMemberName;
    @BindView(R.id.tvMemberType)
    TextView mTvMemberType;
    @BindView(R.id.tvMemberCardNum)
    TextView mTvMemberCardNum;
    @BindView(R.id.tvMemberPhone)
    TextView mTvMemberPhone;
    @BindView(R.id.ivSignature)
    ImageView mIvSignature;
    @BindView(R.id.tvReSignature)
    TextView mTvReSignature;

    private String mOrderSn;
    private OrderDetailEntity mOrderDetailEntity;

    private boolean mBackToMain;

    private Handler mHandler = new Handler();

    public static void startActivity(Context context, String orderSn, boolean backToMain) {
        Intent intent = new Intent();
        intent.setClass(context, OrderDetailActivity.class);
        intent.putExtra("order_sn", orderSn);
        intent.putExtra("back_to_main", backToMain);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_detail);
        ButterKnife.bind(this);

        initData();
        initViews();
    }

    @Override
    protected void onResume() {
        super.onResume();
        loadOrderDetail(mOrderSn);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mHandler.removeCallbacksAndMessages(null);
    }

    @OnClick(R.id.layBack)
    void backClick() {
        if (mBackToMain) {
            MainActivity.startActivity(mContext);
        }
        finish();
    }

    @OnClick(R.id.tvReSignature)
    void confirmClick() {
        SignatureNameActivity.startActivity(mContext, mOrderSn, mOrderDetailEntity.getFood_name(),
                mOrderDetailEntity.getFood_realtotal(), mOrderDetailEntity.getMembers_card());
    }

    @Override
    public void onBackPressed() {
        if (mBackToMain) {
            MainActivity.startActivity(mContext);
        }
        finish();
    }

    private void initData() {
        mOrderSn = getIntent().getStringExtra("order_sn");
        mBackToMain = getIntent().getBooleanExtra("back_to_main", false);
    }

    private void initViews() {
//        loadOrderDetail(mOrderSn);
        mProgressDialog = new ProgressDialog(this);
    }

    private void loadOrderDetail(String orderSn) {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                mProgressDialog.show();
            }
        });
        ApiClient.getApiService()
                .getOrderDetail(ApiClient.getToken(), orderSn)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseEntity<OrderDetailEntity>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mDisposable.add(d);
                    }

                    @Override
                    public void onNext(BaseEntity<OrderDetailEntity> entity) {
                        mProgressDialog.hide();
                        if (entity != null && entity.isSuccess()) {
                            mOrderDetailEntity = entity.getData();
                            showOrderDetailView(mOrderDetailEntity);
                        } else {
                            ToastUtils.showToast(entity.getMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        mProgressDialog.hide();
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void showOrderDetailView(OrderDetailEntity entity) {
        if (entity == null) {
            return;
        }

//        mLayTvTitle.setText("订单详情(" + getTextByPayType(entity.getOrder_type()) + ")");
        mLayTvTitle.setText("订单详情");
        mTvTotalMoney.setText(entity.getFood_total() + "元");
        mTvRealMoney.setText(entity.getFood_realtotal() + "元");
        mTvDiscountAmount.setText(entity.getFood_realprice() + "元");
        mTvBuyAmount.setText(entity.getFood_num() + "L");
        mTvGoodName.setText(entity.getFood_name());
        mTvGoodPrice.setText(entity.getFood_price() + "元/" + entity.getFood_unit());
        mTvDiscountMoney.setText(entity.getFood_amount() + "元/" + entity.getFood_unit());

        mTvMemberType.setText(getMemberType(entity.getMembers_type()) + "-" + getCashModel(entity.getCash_model()));
        mTvMemberName.setText(entity.getUser_name());
        mTvMemberCardNum.setText(entity.getMembers_card());
        mTvMemberPhone.setText(entity.getTel());

        String signPath = entity.getSign_path();
        if (TextUtils.isEmpty(signPath)) {
            mIvSignature.setVisibility(View.GONE);
            mTvReSignature.setVisibility(View.VISIBLE);
        } else {
            mIvSignature.setVisibility(View.VISIBLE);
            mTvReSignature.setVisibility(View.GONE);
            GlideApp.with(mContext)
                    .load(signPath)
                    .override(600, 300)
                    .into(mIvSignature);
        }
    }

    private String getMemberType(int memberType) {
        if (memberType == Constant.Member.MEMBER_PERSON) {
            return "个人用户";
        } else if (memberType == Constant.Member.MEMBER_COMPANY) {
            return "企业用户";
        } else if (memberType == Constant.Member.MEMBER_COMPANY_ADDITIONAL) {
            return "企业用户副卡";
        }
        return "企业用户";
    }

    private String getTextByPayType(int orderType) {
        switch (orderType) {
            case Constant
                    .OrderType.ORDER_FINISHED:
                return "已支付";
            case Constant
                    .OrderType.ORDER_UN_FINISHED:
                return "未支付";
            case Constant
                    .OrderType.ORDER_ABORTED:
                return "已撤单";
        }
        return "";
    }

    private String getCashModel(int cashModel) {
        if (cashModel == 0) {
            return "储蓄卡";
        } else {
            return "后付款";
        }
    }

}
